<?php
class CartController extends Controller{
	public function index(){
//		p($_SESSION);
        $goodsData=Cart::getGoods();
//		p($goodsData);
		
		
		foreach($goodsData as $k=>$v){
		  $gid=$v['id'];
		  $listpic=M("goods")->where("gid='{$gid}'")->getField('listpic');
		  $goodsData[$k]['listpic']=$listpic;
//		  通过查货品列表获得库存
          $sumattr=implode(',',$v['options']);
		  $kucun=M('shoplist')->where("sumattr='{$sumattr}' AND goods_gid='{$gid}'")->getField('kucun');
		  $goodsData[$k]['kucun']=$kucun;
		  
	
			
		}
//		p($goodsData);
		$this->assign('goodsData',$goodsData);
//		将总价分配过来
        $alltotal=Cart::getTotalPrice();
		$this->assign('alltotal',$alltotal);

		
		
		$this->display();
	}
	public function addCart(){
		if(IS_AJAX){
			$gid=$_POST['gid'];
			
			$goodData=M("goods")->where("gid='{$gid}'")->find();
			
			$listpic=$goodData['listpic'];
			
			$des=M("details")->where("goods_gid=$gid")->getField('details');
			$price=M("goods")->where("gid='{$gid}'")->getField('marketprice');
			$num=$_POST['num'];
//			通过sumattr获得属性数组
			$sumattr=$_POST['sumattr'];
			$sumattr=explode(',',$sumattr);
			$shoplist_id=$_POST['shoplist_id'];
			$data=array(
			'id'=>$gid,
			'name'=>$des,
			'num'=>$num,
			'price'=>$price,
			'options'=>$sumattr
		
			);
			
			$sid=Cart::add($data);
//			p($_SESSION);
			echo 1;
			die;
			
			
			
		}
		
	}
	public function updateCart(){
		if(IS_AJAX){
		$sid=$_POST['sid'];
			
		$num=$_POST['num'];
		$data=array(
		'sid'=>$sid,
		'num'=>$num
		);
		
		Cart::update($data);
		$goodsData=Cart::getGoods();

		$goodtotal=$goodsData[$sid]['total'];
	
		$alltotal=Cart::getTotalPrice();
		$data=array(
		'goodtotal'=>$goodtotal,
		'alltotal'=>$alltotal
		);
		
		echo json_encode($data);
		die;
		}

	}
public function delCart(){
	if(IS_AJAX){
		$sid=$_POST['sid'];
		Cart::del($sid);
		$alltotal=Cart::getTotalPrice();
//		p($alltotal);

		
		echo $alltotal;
		die;
	}
}
	
}
 ?>